package com.ruoyi.project.yf.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.yf.domain.vo.MyStockVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.yf.domain.YfStockMy;
import com.ruoyi.project.yf.service.IYfStockMyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 我的股票Controller
 * 
 * @author 云峰
 * @date 2020-06-15
 */
@RestController
@RequestMapping("/yf/my")
public class YfStockMyController extends BaseController
{
    @Autowired
    private IYfStockMyService yfStockMyService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询我的股票列表
     */
    @PreAuthorize("@ss.hasPermi('yf:my:list')")
    @GetMapping("/list")
    @DataScope
    public TableDataInfo list(YfStockMy yfStockMy)
    {
        startPage();
        List<MyStockVo> list = yfStockMyService.selectMyStock(yfStockMy);
        return getDataTable(list);
    }

    @GetMapping("/szzs")
    public  String getSZZSInfo(){
        return redisCache.getCacheObject("SZZS");
    }

    /**
     * 导出我的股票列表
     */
    @PreAuthorize("@ss.hasPermi('yf:my:export')")
    @Log(title = "我的股票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YfStockMy yfStockMy)
    {
        List<YfStockMy> list = yfStockMyService.selectYfStockMyList(yfStockMy);
        ExcelUtil<YfStockMy> util = new ExcelUtil<YfStockMy>(YfStockMy.class);
        return util.exportExcel(list, "my");
    }

    /**
     * 获取我的股票详细信息
     */
    @PreAuthorize("@ss.hasPermi('yf:my:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yfStockMyService.selectYfStockMyById(id));
    }

    /**
     * 新增我的股票
     */
    @PreAuthorize("@ss.hasPermi('yf:my:add')")
    @Log(title = "我的股票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YfStockMy yfStockMy)
    {
        yfStockMy.setCreateBy(SecurityUtils.getUsername());
        yfStockMy.setCreateUid(SecurityUtils.getUserId());
        return toAjax(yfStockMyService.insertYfStockMy(yfStockMy));
    }

    /**
     * 修改我的股票
     */
    @PreAuthorize("@ss.hasPermi('yf:my:edit')")
    @Log(title = "我的股票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YfStockMy yfStockMy)
    {
        yfStockMy.setUpdateBy(SecurityUtils.getUsername());
        yfStockMy.setUpdateUid(SecurityUtils.getUserId());
        return toAjax(yfStockMyService.updateYfStockMy(yfStockMy));
    }

    /**
     * 删除我的股票
     */
    @PreAuthorize("@ss.hasPermi('yf:my:remove')")
    @Log(title = "我的股票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yfStockMyService.deleteYfStockMyByIds(ids));
    }
}
